package com.rockbb.common.utilities.i18n;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class MessageResource {

	private static Logger logger = Logger.getLogger(MessageResource.class);
	private ResourceBundle rb; 

	public MessageResource(ResourceBundle rb) { this.rb = rb; }
	
	public ResourceBundle getResourceBundle() {return rb;}

	public String get(String key) {
		try {
			return rb.getString(key);
		} catch (MissingResourceException ex) {
			logger.info("No such key: "+key);
			logger.debug(ex.getMessage(), ex);
			return key;
		}
	}

	public String get(String key, Object[] args) {
		try {
			String temp = rb.getString(key);
			return MessageFormat.format(temp, args);
		} catch (MissingResourceException ex) {
			logger.info("No such key: "+key);
			logger.debug(ex.getMessage(), ex);
			return key;
		} catch (IllegalArgumentException  ex) {
			logger.info("Illegal arguments for " + key);
			logger.debug(ex.getMessage(), ex);
			return key;
		}
	}
}
